
#define GRP_MOD_MAX_PWR (40 * 1000 * 10 * 10)
#define MAX_PWR         (380 * 1000 * 10 * 10)
uint32_t out_max_pwr[UNIT_NUM] = { 0 };

void mod_limit_pwr1(uint16_t *out_curr)
{
    int i = 0;
    for (i = 0; i < UNIT_NUM; i++)
    {
        out_curr[i] = Mgmt_Info.grp[i].Set_Curr;
        if (Mgmt_Info.grp[i].Pwr == SW_STA_ON)
        {
            uint32_t temp = Mgmt_Info.grp[i].Set_Curr * Pwr_Info.grp[i].info.Volt;
            if (temp > GRP_MOD_MAX_PWR)
            {
                out_curr[i] =
                    (uint32_t)(GRP_MOD_MAX_PWR * 0.95 / Pwr_Info.grp[i].info.Volt);
            }
        }
    }
}

void mod_limit_pwr(uint16_t *out_curr)
{
    uint32_t total_req_pwr = 0;
    int      i = 0;

    float limit_percent = 1.0;

    for (i = 0; i < UNIT_NUM; i++)
    {
        if (Mgmt_Info.grp[i].Pwr == SW_STA_ON)
        {
            out_max_pwr[i] = Mgmt_Info.grp[i].Set_Curr * Pwr_Info.grp[i].info.Volt;
            if (out_max_pwr[i] > GRP_MOD_MAX_PWR)
            {
                out_max_pwr[i] = GRP_MOD_MAX_PWR;
            }
            total_req_pwr = total_req_pwr + out_max_pwr[i];
        }
        else
        {
            out_max_pwr[i] = 0;
        }
    }

    if (total_req_pwr > MAX_PWR)
    {
        limit_percent = (float)MAX_PWR / total_req_pwr;
    }

    for (i = 0; i < UNIT_NUM; i++)
    {
        if (Mgmt_Info.grp[i].Pwr == SW_STA_ON && Mgmt_Info.grp[i].Set_Curr > 50 && Pwr_Info.grp[i].info.Volt > 0)
        {
            out_max_pwr[i] = (uint32_t)(out_max_pwr[i] * limit_percent);
            out_curr[i] = out_max_pwr[i] / Pwr_Info.grp[i].info.Volt;
            out_curr[i] = out_curr[i] > Mgmt_Info.grp[i].Set_Curr
                              ? Mgmt_Info.grp[i].Set_Curr
                              : out_curr[i];
        }
        else
        {
            out_curr[i] = Mgmt_Info.grp[i].Set_Curr;
        }
    }
}